package doInterce.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import doInterce.DoInterface;
import utils.PathUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: syh
 * @Date: 2023/12/25/9:56
 */
public class DoPeopleDif implements DoInterface {
    @Override
    public void doExecute() {
        String jarPath = PathUtil.getJarPath();
        jarPath = jarPath.replaceAll("parsing_excel.jar","");
        System.out.println(jarPath);
        File file1 = new File(jarPath+"表一.txt");
        File file2 = new File(jarPath+"表二.txt");
        File result1 = new File(jarPath+"结果-带姓名.txt");
        File result2 = new File(jarPath+"结果-不带姓名.txt");

//        File file1 = new File("C:\\Users\\Yi\\Desktop\\aa\\表一.txt");
//        File file2 = new File("C:\\Users\\Yi\\Desktop\\aa\\表二.txt");
//        File result1 = new File("C:\\Users\\Yi\\Desktop\\aa\\结果-带姓名.txt");
//        File result2 = new File("C:\\Users\\Yi\\Desktop\\aa\\结果-不带姓名.txt");


        Map<String,String> map1 = new HashMap<>();
        FileUtil.readLines(file1,"UTF-8").forEach(line->{
            if(StrUtil.isEmpty(line)){
                return;
            }
            String[] split = line.split("\t");
            map1.put(split[1],split[0]);
        });

        Map<String,String> map2 = new HashMap<>();
        FileUtil.readLines(file2,"UTF-8").forEach(line->{
            if(StrUtil.isEmpty(line)){
                return;
            }
            String[] split = line.split("\t");
            map2.put(split[1],split[0]);
        });

        System.out.println("aaa");
        List<String> keyList1 = map1.keySet().stream().filter(ele -> !map2.containsKey(ele)).collect(Collectors.toList());
        List<String> keyList2 = map2.keySet().stream().filter(ele->!map1.containsKey(ele)).collect(Collectors.toList());

        List<String> contentList1 = new ArrayList<>();
        List<String> contentList2 = new ArrayList();

        contentList1.add("表一比表二多的：");
        contentList2.add("表一比表二多的：");
        for(String key:keyList1){
            contentList1.add(map1.get(key)+" "+key);
            contentList2.add(key);
        }

        contentList1.add("");
        contentList2.add("");
        contentList1.add("-------------");
        contentList2.add("-------------");
        contentList1.add("");
        contentList2.add("");
        contentList1.add("表二比表一多的：");
        contentList2.add("表二比表一多的：");
        for(String key:keyList2){
            contentList1.add(map2.get(key)+" "+key);
            contentList2.add(key);
        }

        try {
            if (!result1.exists()) {
                result1.createNewFile();
            }
            if (!result2.exists()) {
                result2.createNewFile();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        FileUtil.writeLines(contentList1,result1,"UTF-8");
        FileUtil.writeLines(contentList2,result2,"UTF-8");

        System.out.println("执行完成！");
    }
}
